package com.cskaoyan.market.controller;


import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.AdminAddressService;
import com.cskaoyan.market.service.impl.AdminAddressServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/address/*")
public class AdminAddressController extends HttpServlet {

    AdminAddressService adminAddressService = new AdminAddressServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/address/", "");
        switch (replace) {
            case "list":
                list(req,resp);
                break;
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String name = req.getParameter("name");
        String userId = req.getParameter("userId");
        List<MarketAddress> addressList = adminAddressService.list(page,limit,sort,order,name,userId);
        Object o = ResponseUtil.okList(addressList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
